<?php
/*
session
ACL
Auth
Cache
*/

$app_path = realpath(dirname(__FILE__) . '/../');
$lastchar = substr($app_path, -1);
if($lastchar == '/' || $lastchar == "\\")
	define('APP_PATH', $app_path);
else
	define('APP_PATH', $app_path . "/");

include "Zend/Loader.php";
Zend_Loader::registerAutoload();
//decide on site name

// SITE_PATH is the
if (file_exists(APP_PATH . "sitedata/" . $_SERVER['HTTP_HOST'] . "/")) {
	define('SITE_PATH', APP_PATH . "sitedata/" . $_SERVER['HTTP_HOST'] . "/");
	define('CACHE_PATH', APP_PATH . "sitecache/" . $_SERVER['HTTP_HOST'] . "/");
}
else
{
	define('SITE_PATH', APP_PATH . "sitedata/default/");
	define('CACHE_PATH', APP_PATH . "sitecache/default/");
}

// load config
if (file_exists(SITE_PATH . "msite_config.ini")) {
	$msite_config = new Zend_Config_Ini(SITE_PATH . 'msite_config.ini', null);	// load everything
	// setup registry
	$registry = Zend_Registry::getInstance();
	$registry->set('msite_config', $msite_config);
}
else
{	// error
	print "<h1>msite_config.ini missing</h1>";
	exit();
}

// load config
if (file_exists(SITE_PATH . "config.ini")) {
	$site_config = new Zend_Config_Ini(SITE_PATH . 'config.ini', 'main');	// load everything
	// setup registry
	$registry->set('site_config', $site_config);
}
else
{
	print "<h1>config.ini missing</h1>";
	exit();
}

date_default_timezone_set($site_config->tz);

// Session
$session = new Veneer_Session();
$registry->set('veneer_session', $session);

// CACHE

//print $site_config->debugMode;
$debug = new Veneer_Debug($site_config->debugMode);
$registry->set('veneer_debug', $debug);

$cache = new Veneer_Cache();
$registry->set('veneer_cache', $cache);
if($msite_config->main->cacheOn==1) {
	if(($pg = $cache->pgLoad())) {

		echo $pg;

		$debug->comment("Cached");
		print $debug->echoLog();
		exit();
	}
}
//STOP CACHE


//setup database
$db = Zend_Db::factory($msite_config->database->adapter, $msite_config->database->cfg->toArray());
Zend_Db_Table::setDefaultAdapter($db);
$registry->set('dbAdapter', $db);	// for auth?
// setup Log but need to include modes *****************************************
$veneerLog = new Veneer_Log();
$registry->set('veneerLog', $veneerLog);

// module/action
$router = new Zend_Controller_Router_Rewrite();
$route = new Zend_Controller_Router_Route(
	':module/:action/:p1/:p2/:p3/:p4/:p5/:p6/*',
	array(
		'controller'=>'index',
		'p1'=>null,
		'p2'=>null,
		'p3'=>null,
		'p4'=>null,
		'p5'=>null,
		'p6'=>null,
		'action'=>'index'
	)
);
$router->addRoute('default',$route);

$router->addRoute('admin',
	new Zend_Controller_Router_Route(
	'admin/:module/:action/*',
	array(
		'controller'=>'admin',
		'action'=>'index',
		'module'=>'default',
		'p1'=>null,
		'p2'=>null,
		'p3'=>null,
		'p4'=>null,
		'p5'=>null,
		'p6'=>null
	)
));

$router->addRoute('my',
	new Zend_Controller_Router_Route(
	'my/:module/:action/*',
	array(
		'controller'=>'my',
		'action'=>'index',
		'module'=>'default',
		'p1'=>null,
		'p2'=>null,
		'p3'=>null,
		'p4'=>null,
		'p5'=>null,
		'p6'=>null
	)
));

$router->addRoute('auto',
	new Zend_Controller_Router_Route(
	'auto/:module/:action/*',
	array(
		'controller'=>'auto',
		'action'=>'index',
		'module'=>'default'
	)
));
/* add later
$router->addRoute('login',
	new Zend_Controller_Router_Route(
	'admin/:module/:action/*',
	array(
		'controller'=>'index',
		'action'=>'login',
		'module'=>'user',
	)
));
$router->addRoute('logout',
	new Zend_Controller_Router_Route(
	'admin/:module/:action/*',
	array(
		'controller'=>'index',
		'action'=>'logout',
		'module'=>'user'
	)
));
*/

// Setup Layout Subsystem.
//************* controller  must equal index
if($site_config->layoutOn == 1)
{  // move to Veneer_Controller_Module when I figure out why it does not work
// make sure it is module controller

	Zend_Layout::setup(array('path' => APP_PATH . '/themes/' . $site_config->theme . '/layouts/'));

	//Configure the viewrenderer/layout system to render content into index.phtml automatically
	Zend_Layout::setDefaultLayoutName('index');

	//Configure an alternative layout with default elements
	//************************************** element - action, controller, module
	Zend_Layout::getLayout('index')
	        ->addRequest(new Zend_Layout_Request('sideBlockZone', 'loginToggle', 'block','user'));	
	Zend_Layout::getLayout('index')
	        ->addRequest(new Zend_Layout_Request('leftMenuZone', 'icommenu', 'block','default'));
	Zend_Layout::getLayout('index')
	        ->addRequest(new Zend_Layout_Request('leftMenuZone', 'devmenu', 'block','default'));
}
Zend_Loader::loadFile('Plugin.php','Veneer');	// handles autocaching

